﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-06-09 14:12:58
 * 功能：插屏广告
**************************************************************************************/

using Game.Framework;

public class Interstitial : AdBase
{
    public Interstitial(string adUnitId) : base(adUnitId)
    {
        m_AdType = EAdType.Interstitial;
    }

    public override void Initialize()
    {
        // 监听事件
        MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnLoadedEvent;
        MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnLoadFailedEvent;
        MaxSdkCallbacks.Interstitial.OnAdDisplayedEvent += OnDisplayedEvent;
        MaxSdkCallbacks.Interstitial.OnAdDisplayFailedEvent += OnAdFailedToDisplayEvent;
        MaxSdkCallbacks.Interstitial.OnAdClickedEvent += OnClickedEvent;
        MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnHiddenEvent;
        MaxSdkCallbacks.Interstitial.OnAdRevenuePaidEvent += OnAdRevenuePaidEvent;
        LoadAd();
    }

    protected override void LoadAd()
    {
        base.LoadAd();
        // 加载插页广告
        MaxSdk.LoadInterstitial(m_AdUnitId);
        LogManager.Log($"加载Max广告，type:{m_AdType}", "广告");
    }

    protected override void OnAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo info)
    {
        m_TotalReward = GameDataMgr.Instance.IntAdTotalRevenue;
        base.OnAdRevenuePaidEvent(adUnitId, info);
        GameDataMgr.Instance.IntAdTotalRevenue = m_TotalReward;
    }

    public override void Show(ETriggerAdType type)
    {
        m_TriggerType = type;
        if (MaxSdk.IsInterstitialReady(m_AdUnitId))
        {
            EventCenter.Instance.Trigger(GameEventName.ON_BE_ABOUT_TO_SHOW_AD, m_AdType, m_TriggerType);
            MaxSdk.ShowInterstitial(m_AdUnitId);
            LogManager.Log($"开始Max插屏广告播放,adUnitID:{m_AdUnitId}", "广告");
        }
        else
        {
            EventCenter.Instance.Trigger(GameEventName.ON_SHOW_FAILED_AD, m_AdType, m_TriggerType);
            LogManager.Log($"Max插屏广告未准备好，无法播放！！", "广告");
        }
    }
}